package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberMapper;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.dao.SetMealMapper;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@DubboService
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            Integer num = memberMapper.count(month + ".31");
            memberCount.add(num);
        }
        return memberCount;
    }

    @Override
    public List<Map<String, Object>> getSetmealReport() {
        List<Map<String, Object>> reportlist = setMealMapper.getSetmealReport();
        return reportlist;
    }

    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> map = new HashMap<>();

        //获取当前时间
        //将时分秒毫秒清零
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        //将周一设置为本周第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);

        //设置日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //本周第一天
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //本周最后一天
        Calendar weekLastDay = (Calendar) today.clone();
        weekLastDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        //本月第一天
        Calendar monthFirstDay = (Calendar) today.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        //本月最后一天
        Calendar monthLastDay = (Calendar) today.clone();
        int actualMaximum = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH, actualMaximum);

        //日格式化
        String todayStr = sdf.format(today.getTime());
        String weekFirstDayStr = sdf.format(weekFirstDay.getTime());
        String weekLastDayStr = sdf.format(weekLastDay.getTime());
        String monthFirstDayStr = sdf.format(monthFirstDay.getTime());
        String monthLastDayStr = sdf.format(monthLastDay.getTime());

        //返回数据结果--当前日期
        /*  reportDate:null,*/
        map.put("reportDate", todayStr);

        /*
                    totalMember :0,
                    todayNewMember :0,
                    thisWeekNewMember :0,
                    thisMonthNewMember :0,
                   */
        Integer totalMember = memberMapper.getTotalCount();
        Integer todayNewMember = memberMapper.countRangRegtime(todayStr, todayStr);
        Integer thisWeekNewMember = memberMapper.countRangRegtime(weekFirstDayStr, weekLastDayStr);
        Integer thisMonthNewMember = memberMapper.countRangRegtime(monthFirstDayStr, monthLastDayStr);
        map.put("totalMember", totalMember);
        map.put("todayNewMember", todayNewMember);
        map.put("thisWeekNewMember", thisWeekNewMember);
        map.put("thisMonthNewMember", thisMonthNewMember);

        /*          todayOrderNumber :0,
                    thisWeekOrderNumber :0,
                    thisMonthOrderNumber :0,

                    todayVisitsNumber :0,
                    thisWeekVisitsNumber :0,
                    thisMonthVisitsNumber :0,
                    */
        Integer todayOrderNumber = orderMapper.getOrderNumber(todayStr, todayStr, null);
        Integer thisWeekOrderNumber = orderMapper.getOrderNumber(weekFirstDayStr, weekLastDayStr, null);
        Integer thisMonthOrderNumber = orderMapper.getOrderNumber(monthFirstDayStr, monthLastDayStr, null);
        map.put("todayOrderNumber", todayOrderNumber);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);

        Integer todayVisitsNumber = orderMapper.getOrderNumber(todayStr, todayStr, Order.ORDERSTATUS_YES);
        Integer thisWeekVisitsNumber = orderMapper.getOrderNumber(weekFirstDayStr, weekLastDayStr, Order.ORDERSTATUS_YES);
        Integer thisMonthVisitsNumber = orderMapper.getOrderNumber(monthFirstDayStr, monthLastDayStr, Order.ORDERSTATUS_YES);
        map.put("todayVisitsNumber", todayVisitsNumber);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);

        /*hotSetmeal :[
                        {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
                        {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
                    ]*/
        List<Map<String, String>> hotSetmealList = orderMapper.gethotSetmeal();
        map.put("hotSetmeal", hotSetmealList);
        return map;
    }
}
